<template>
  <div class="container">
    <div class="select-container">
     <el-row class="">
        <el-col :span="24" class="">
          <el-form ref="form" label-width="100px" label-position='right' size="small" :inline="true">
             <el-col :span="4" style="">
                    <el-form-item label="" style="margin:5px 3px;">
              <el-select placeholder="请选择业务系统"  v-model="systemId" clearable>
                <el-option v-for="item in systemList" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
              </el-select>
              </el-form-item>
                  </el-col>
                  <el-col :span="5" style="">
                    <el-form-item label="" style="margin:5px 3px;">
              <el-input
                size="small"
                style="width: 200px;"
                class="filter-item"
                placeholder="请输入登录名称"
                clearable
                v-model="loginName"
                prefix-icon="el-icon-search"
              >
              </el-input>
              </el-form-item>
                  </el-col>
                  <el-col :span="6" style="">
                    <el-form-item label="" style="margin:2px -5px;">
              <el-button size="small" type="success" @click="fetchData()" style="margin-right: 10px">查 询</el-button>
              </el-form-item>
                  </el-col>
           </el-form>
          </el-col>
      </el-row>
    </div>
    <div class="list-container">
      <div class="filter">
        <el-row :gutter="20">
          <el-col :span="10">
            <span style="line-height: 33px;padding-left:10px;">用户绑定认证管理</span>
          </el-col>
          <el-col :span="14" style="text-align: right;">
            <el-button size="small" type="primary" @click="createClick()" style="margin-right: 10px">创建用户绑定</el-button>
          </el-col>
        </el-row>
      </div>
      <div class="data-contant">
        <div>
          <el-table
            :key='tableKey'
            :data="list.records"
            v-loading="listLoading"
            element-loading-text="给我一点时间"
            size="small"
            stripe
            style="width: 100%;border:#eeeeee"
            highlight-current-row>
            <el-table-column
              align="center"
              type="index"
              width="80"
              label="编号"
            >
            </el-table-column>
            <el-table-column
              align="center"
              label="系统名称"
              prop="systemName"
            >
            </el-table-column>

            <el-table-column
              align="center"
              label="登录名称"
              prop="loginName"
            >
            </el-table-column>
            <el-table-column
              align="center"
              label="第三方系统登录名称"
              prop="thirdLoginName"
            >
            </el-table-column>


            <el-table-column align="center" label="操 作" width="290px" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
                <el-button type="primary" size="small" @click="handleDelete(scope.row)">删除</el-button>

              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <el-pagination
              layout=" total, prev, pager, next"
              :page-size = "this.postList.rowBounds.limit"
              :total="this.list.count"
              @current-change="current_change"
            >
            </el-pagination>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import '@/styles/newstyle.scss'
import {
  systemUserMappingCompositeSelect,
  systemUserMappingDelete
} from '@/api/bussSystemManage/systemUserMapping'

import {
  selectSysSubsys
} from '@/api/systemManage/roleManagement'

export default {
  data() {
    return {
     systemId:'',
     loginName:'',
     systemList:[],
     dropdownParams: {
        condition: [{
          'property': 'status',
          'value': '2',
          'type': 'NE'
        },{
          'property': 'userId',
          'value': this.$store.getters.userid,
          'type': 'EQ'
        }]
      },
       postList: {
        condition: [],
        sort: [{
          direction: 'DESC',
          property: 'editTime'
        }],
        rowBounds: {
          offset: 0,
          limit: 10
        }
      },
      list: {},

      tableKey: 0,



      listLoading: false,

      deletelist: {}
    }
  },
  // 画面刚进入method
  created() {
    this.getDropdownList()
    this.loadSystemScopeCondition()
    this.fetchData()

  },
  methods: {
    loadSystemScopeCondition(){
      var array=[]
      this.systemList.forEach(function(item, index, arr) {
          array.push(item.systemId)
      })

      this.postList.condition.push({
          'property': 'systemId',
          'value': array,
          'type': 'ENUM'
        });
    },
    getDropdownList() {

      this.loading = true

      const query = {
        sort: [
          {
            direction: 'ASC',
            property: 'id'
          }
        ]
      }

      selectSysSubsys(query).then(response => {
        const records = response.data.data.records
        if (response.data.data.count > 0) {
          this.systemList = []
          for (const item of records) {
            const input = {}
            input.id = item.id
            input.name = item.name
            this.systemList.push(input)
            console.log(this.systemList)
          }
        }
      })
    },
    fetchData(){


      this.loading = true

      this.postList.condition.forEach(function(item, index, arr) {
          if(item.property == "systemId") {
              arr.splice(index, 1);
          }
      })
      if(this.systemId!=""){

        this.postList.condition.push({
          'property': 'systemId',
          'value': this.systemId,
          'type': 'EQ'
        });
      }

      this.postList.condition.forEach(function(item, index, arr) {
          if(item.property == "loginName") {
              arr.splice(index, 1);
          }
      })

      if(this.loginName!=""){
      this.postList.condition.push({
          'property': 'loginName',
          'value': this.loginName,
          'type': 'LIKE'
        });
      }

      systemUserMappingCompositeSelect(this.postList).then(res => {
        this.list = res.data.data
        this.list.records = res.data.data.records
        this.loading = false
      }).catch((err) => {
        console.log(err)
      })
    },





    deleteData() {
      systemUserMappingDelete(this.deletelist).then(res => {
        this.fetchData()
      }).catch((err) => {
        console.log(err)
      })
    },
    handleDelete(row) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deletelist.id = row.id
        this.deleteData()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    createClick() {
      this.$router.push({
        name: 'systemUserMappingAdd'
      })
    },
    handleEdit(row) {
      this.$router.push({
        name: 'systemUserMappingUpdate',
        params: {
          list: {

          id: row.id,
          systemId: row.systemId,
          loginName: row.loginName,
          thirdLoginName: row.thirdLoginName,
          thirdPassword: row.thirdPassword

          }
        }
      })
    },
    current_change(currentPage) { // 通过分页获取数据
      const page = currentPage - 1
      this.postList.rowBounds.offset = page * 10
      this.fetchData()
    },


  }
}
</script>

<style scoped>
  .container{
    color: #3b96f3;
    background: #fff;
    padding: 20px;
    height: 100%;
    overflow-y: auto;
  }
  .select-container{
    padding: 10px 10px;
    margin-bottom: 10px;
    border-radius: 4px;
    border: 1px solid #eeeeee;
  }
  .list-container{
    margin-top:30px;
    padding:0 10px 10px 10px;
    border:1px solid#eeeeee;
    border-radius: 4px;
  }
  .data-contant{
    background-color: rgba(0,0,0,.035);
    padding: 30px 10px 20px;
  }
  .data-detail{
    font-size: 14px;
    padding:20px 0;
    text-align: center;
  }
  .data-title{
    font-size: 14px;
    padding:10px 0;
    text-align: center;
  }
  .data-title div{
    font-size: 14px;
    padding:5px 0;
  }
  .filter{
    padding: 10px 5px;
    border-bottom: 2px solid#eeeeee;
  }
  .list-container{
  }
  .bold{
    font-weight: bold;
  }
</style>
